package com.linewell.commom.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linewell.commom.model.FeedBack;
import com.linewell.commom.service.IFeedBackService;
import com.linewell.system.model.SysNotice;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.pagemodel.ReturnInfo;

/**
 * 客户评价控制器类
 */
@Controller
@RequestMapping("/manage/feedBack")
public class FeedBackController extends BackendBaseController {
	private static final Log log = LogFactory.getLog(FeedBackController.class);

	@Autowired
	private IFeedBackService feedBackService;

	/**
	 * 
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {

		return super.index("/WEB-INF/pages/common/feed_back/feed_back_list.jsp", request);
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute FeedBack c, PageHelper ph,String begin_time,String end_time) {
		
		ph.setSort("createTime");
		ph.setOrder("desc");
		DataGrid dg = feedBackService.datagrid(ph,c,begin_time,end_time);
		return dg;
	}
	/**
	 * 添加
	 */
	@RequestMapping(value="/add")
	public String add(Map<String, Object> map){
		StringBuffer bottombase = new StringBuffer();
		bottombase.append("<div data-am-widget=\"navbar\" class=\"am-navbar  am-navbar-success gm-foot\"   id=\"\">");
		bottombase.append("<ul class=\"am-navbar-nav am-cf am-avg-sm-4\">");
		bottombase.append(
				"<li ><a href=\"/mobile/\" target=\"_self\" class=\"curr\"> <span class=\"am-icon-home\"></span> <span class=\"am-navbar-label\">首页</span> </a></li>");
		bottombase.append(
				"<li> <a href=\"/mobile/market\" target=\"_self\"  class=\"curr\"> <span class=\"am-icon-list\"></span> <span class=\"am-navbar-label\">二手市场</span> </a> </li>");
		bottombase.append(
				"<li > <a href=\"/manage/feedBack/add\" target=\"_self\" class=\"curr\"> <span class=\"am-icon-location-arrow\"></span> <span class=\"am-navbar-label\">留言板</span> </a> </li>");
		bottombase.append(
				"<li > <a href=\"/mobile/gomy\" target=\"_self\" class=\"curr\"> <span class=\"am-icon-user\"></span> <span class=\"am-navbar-label\">我的</span> </a> </li>");
		bottombase.append("</ul> </div>");
		map.put("saveUrl", "/manage/feedBack/addSave");
		map.put("bottom", bottombase.toString());
		return "/WEB-INF/pages/mobile/main/feedback.jsp";
	}
	
	/**
	 * 添加保存
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid FeedBack c, BindingResult b, ReturnInfo result,HttpServletRequest request) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getField() + "" + error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (c != null) {
				System.out.println(c);
				try{
				feedBackService.save(c);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}
			}
		}
		return result;
	}
	

	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		map.put("saveUrl", "/manage/feedBack/editSave");
		map.put("id", id);
		return "/WEB-INF/pages/common/feed_back/feed_back_au.jsp";
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid FeedBack c, BindingResult b, ReturnInfo result,HttpServletRequest request) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (c != null) {
				try {
					//c.getSysUsers().setUserId(getLoginUserId(request));出bug了
					feedBackService.update(c,getLoginUserId(request));
				} catch (Exception e) {
					log.info("修改异常" + e.getMessage());
					result.setMsg("修改失败");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}

	/**
	 * 移除,逻辑删除，将状态修改为0
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String ids, ReturnInfo result) {

		if (ids != null) {
			try {
				feedBackService.removeAll(ids);
			} catch (Exception e) {
				log.info("移除异常" + e.getMessage());
				result.setMsg("移除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}

	
	
}
